03. Creating Variables in Swift
Declaring Variables
Primitive Types
Variables and Constants
Closely related to variables are constants. Use of constants can be summarized with the following phrase: “if the value does not change, then use a constant instead of a variable”. You may also hear constants referred to as immutable. When something is immutable it does not change. Likewise, if something can change (like a variable) then it is mutable.
Let's imagine we want to use the (approximated) value for pi (π) which is 3.14. This value is constant and should not change. Right now, we may be inclined to declare pi as a variable…
var pi = 3.14
But this is not the behavior we want. Instead, we should declare a constant using the following syntax:
let nameOfConstant = valueOfConstant
And we would declare pi specfically like so:
let pi = 3.14
Because pi is declared as a constant, its value cannot change. If we try to change its value, then the Swift compiler will complain.
The compiler recognizes that a constant should not be allowed to change.
Try creating a constant, and then try adjusting its value. It should fail.
Declaring a Variable with a Variable
Let’s experiment a little more with variables and constants. When we declare a variable or constant we can actually use another variable or constant as the value. For example, let’s say we have a variable called myFavoriteBaseballTeam which is the Atlanta Braves. Also, let’s say my twin brother James’ favorite team is the Atlanta Braves. I can create a variable for jamesFavoriteBaseballTeam and set it equal to myFavoriteBaseballTeam.
In this example, jamesFavoriteBaseballTeam is equal to myFavoriteBaseTeam… which are the Atlanta Braves.
And if we look at their values they are the same. But, be careful…
Changing jamesFavoriteBaseballTeam does not effect myFavoriteBaseballTeam.
If I change jamesFavoriteBaseballTeam to the Minnesota Twins, then notice how myFavoriteBaseballTeam does not change. This is because we just copied the value when we created jamesFavoriteBaseballTeam. After the value was copied, each variable can change independently.
Declaring a Variable with a Constant
We can also set a variable equal to a constant.
A variable can be assigned to a constant, but a constant (like theBaseballTeamInAtlanta) still cannot change.
But as you might expect, we still cannot change the value of a constant. A constant is constant—it does not change.
Declaring a Constant with a Variable
We can also declare a constant with a value from a variable. For example:
```swift
var jamesFavoriteBaseballTeam = "Atlanta Braves"
let johnsFavoriteBaseballTeam = jamesFavoriteBaseballTeam```
If we try to change the value of a constant after it has been declared, then the Swift compiler will give us an error:
A constant's value can not change, but it is possible to change a constant into a variable.
The compiler doesn't just stop there. If you click on the error, it actually tries to help us! It says: Change 'let' to 'var' to make it mutable, and it even shows the suggested change in our code. If we were to hit 'enter' here, the compiler would make the change for us. But this is a bad idea… John wouldn't like anyone trying to change his opinion about his favorite team. So we'll just leave it as a constant.
Declaring a Constant with a Constant
Now it's your turn. Try creating a constant using the value of another constant. _Need some tips to do this? Ask our coaches on the forums.